package com.h.chatroom.controller;

import com.h.chatroom.pojo.request.UserLoginRequest;
import com.h.chatroom.pojo.request.UserRegisterRequest;
import com.h.chatroom.pojo.response.UserInfoResponse;
import com.h.chatroom.pojo.response.UserLoginResponse;
import com.h.chatroom.pojo.response.UserRegisterResponse;
import com.h.chatroom.service.UserService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource(name = "userServiceImpl")
    private UserService userService;

    // 登录
    @RequestMapping("/login")
    @ResponseBody
    // @RequestBody注解可以将JSON数据自动映射到相应的Java对象。
    public UserLoginResponse login(@RequestBody @Validated UserLoginRequest userLoginRequest) {
      log.info("用户进行登录，用户名为username: " + userLoginRequest.getUsername());
      return userService.login(userLoginRequest);
    }

    // 注册
    @RequestMapping("/register")
    @ResponseBody
    public UserRegisterResponse register(@RequestBody UserRegisterRequest userRegisterRequest) {
        log.info("用户注册，用户名为username: " + userRegisterRequest.getUsername());
        return userService.register(userRegisterRequest);
    }

    // 获取登录用户的信息，根据登录用户的ID
    @RequestMapping("/getUserInfo")
    @ResponseBody
    public UserInfoResponse getUserInfo(@NotNull(message = "用户Id不能为空") Integer userId) {
        log.info("获取用户信息，用户ID为: " + userId);
        return userService.getUserInfo(userId);
    }
}
